<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Carrera extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url', 'form'));
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Carrera_Model');
    }

    public function index() {

        if (!$this->ion_auth->logged_in()) {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        } elseif (!$this->ion_auth->is_admin()) {
            //redirect them to the home page because they must be an administrator to view this
            $this->_render('home/inicio');
        } else {
            //set the flash data error message if there is one
            $this->_render('home/inicio');
        }
    }

    public function inicio() {
        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $this->_render('carrera/start');
    }

    public function crear() {
        $this->load->library('form_validation');
        //print_r($this->data['institucion']);
        $this->_render('carrera/new');

        //redirect('auth', 'refresh');
    }

    public function actualizar() {
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');

        $opc = $this->uri->segment(3);
        $ide = $this->uri->segment(4);
        if (!isset($_POST['txtcod'])) {
            $this->data['carrera'] = $this->Carrera_Model->getDatosCarrera($ide);
            $this->data['opc'] = $opc;
            $this->_render('carrera/update');
        } else {
            $ide = $this->input->post('txtide');
            $opc = $this->input->post('txtopc');
            $this->data['carrera'] = $this->Carrera_Model->getDatosCarrera($ide);
            $this->data['opc'] = $opc;

            $this->form_validation->set_rules('txtcod', 'Codigo de la Carrera', 'required|trim');
            $this->form_validation->set_rules('txtcodarea', 'Area del Conocimiento', 'required|trim');
            $this->form_validation->set_rules('cod_subac', 'Subarea del Conocimiento', 'required|trim');
            $this->form_validation->set_rules('txtnombre', 'Nombre de la Carrera', 'required|trim');

            if (($this->form_validation->run() == FALSE)) {
                $this->_render('carrera/update');
                //redirect("institucion/actualizar/".$opc."/".$ide);
            } else {


                $carrera = array(
                    "cod_car" => $this->input->post('txtcod'),
                    "cod_areac" => $this->input->post('txtcodarea'),
                    "cod_subac" => $this->input->post('cod_subac'),
                    "nombre_car" => $this->input->post('txtnombre'),
                    "estado_car" => $this->input->post('estado'),
                    "duracion_car" => $this->input->post('duracion'));


                if ($opc == 'upd')
                    $this->Carrera_Model->actualizarCarrera($carrera, $ide);
                else
                    $this->Carrera_Model->eliminarCarrera($ide);
                redirect('carrera/inicio');
            }
        }
    }

    function presearch() {
        $who = $this->input->post('txtsearch');

        if (empty($who))
            redirect("carrera/inicio");
        else {
            $this->data['carrera'] = $this->Carrera_Model->getBusquedaCarrera($who);
            $this->_render('carrera/start');
        }
    }

    public function ingresodatos() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('txtcod', 'Codigo de la Carrera', 'required|trim');
        $this->form_validation->set_rules('txtcodarea', 'Codigo del Area del Conocimiento', 'required|trim');
        $this->form_validation->set_rules('cod_subac', 'Subarea del Conocimiento', 'required|trim');
        $this->form_validation->set_rules('txtnombre', 'Nombre de la Carrera', 'required|trim');

        if (($this->form_validation->run() == FALSE)) {

            $this->_render('carrera/new');
        } else {

            $carrera = array(
                "cod_car" => $this->input->post('txtcod'),
                "cod_areac" => $this->input->post('txtcodarea'),
                "cod_subac" => $this->input->post('cod_subac'),
                "nombre_car" => $this->input->post('txtnombre'),
                "estado_car" => $this->input->post('estado'),
                "duracion_car" => $this->input->post('duracion'));

            $this->Carrera_Model->grabarCarrera($carrera);
            redirect('carrera/inicio');
        }
    }

}

/* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */


    